package com.example.linyiquan.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;

import com.example.linyiquan.app.R;

/**
 * Created by linyiquan on 17-4-14.
 */

public class WelcomeActivity extends Activity {

    private boolean isfirst = false;

    private static final int Time = 2000;
    private static final int Go_Home = 2000;
    private static final int Go_Guide = 2001;

    /**
     * 多线程
     */
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case Go_Home:
                    goHome();
                    break;
                case Go_Guide:
                    goGuide();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);

        init();
    }

    /**
     * 进行是否第一次进入这个应用
     */
    private void init() {
        SharedPreferences sharedPreferences = getSharedPreferences("welcome", MODE_PRIVATE);
        isfirst = sharedPreferences.getBoolean("first", true);
        if (!isfirst) {
            handler.sendEmptyMessageDelayed(Go_Home, Time);
        } else {
            handler.sendEmptyMessageDelayed(Go_Guide, Time);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("first", false);
            editor.commit();
        }
    }

    /**
     * 跳转到登陆界面
     */
    private void goHome() {
        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 跳转到引导页界面
     */
    private void goGuide() {
        Intent intent = new Intent(WelcomeActivity.this, GuideActivity.class);
        startActivity(intent);
        finish();
    }

}
